import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydae -o ydae.json
https://301.sexav-301.com/ssss
'''


class YdaeSpider(scrapy.Spider):
    name = "ydae"
    # 前缀
    prefix = 'https://301.'
    # 中缀
    website = 'sexav-301'
    # 后缀
    suffix = '.com/'
    allowed_domains = [website + '.com']
    start_urls = ['https://301.sexav-301.com/ssss']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, url=response.url, name='', pUrl='', vUrl=format_url_one(video_url[0]))
        tag_list = response.xpath("//a[@class='w4em mr10 tx-radius ov-h tx-flex-sh']/@title").extract()
        name_list = response.xpath("//h1[@class='f-20 f-bold tx-c2 mb5 tx-flex-sh']/text()").extract()
        url_list = response.xpath("//span[@class='tx-flex-sh']/a/@href").extract()
        pic_list = response.xpath("//div[@class='detail-image-wrapper col-10 col-m-24']/img/@src").extract()
        if len(tag_list) and len(name_list) and len(url_list) and len(pic_list) and len(pic_list):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tag_list[0], url=split_joint(self.prefix + self.website + self.suffix, url_list[0]), name=name_list[0], pUrl=pic_list[0])
        # 从网页中提取url链接
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('http') or url.startswith('www'):
                yield scrapy.Request(url, callback=self.parse)
